package com.music.action.authority;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.music.action.BaseAction;
import com.music.action.BaseKey;
import com.music.authority.service.UpVedioService;
import com.music.common.ParamUtil;

/**
 * 上传视频相关action
 * @author LFSenior
 *
 */
public class UpVedioAction extends BaseAction{
	@Autowired
	UpVedioService upVedioService;
	
	private String name;
	private String author;
	private String vedioFileName;
	private File image;
	private String imageFileName;
	private File vedio;
	private String vedioContentType;
	/**
	 * 跳转到上传视频页面
	 * @return
	 */
	@Action(value="upvedio",results={@Result(name="upvedio",location="authority/upvedio.jsp")})
	public String redicterUpVedio(){
		request.put(BaseKey.AUTHORITY_VIDEO_LIST, upVedioService.findAllVedio());
		return "upvedio";
	}
	
	@Action(value="delVideo",results={@Result(name=POST,type="json",params={"root","reParam"})})
	public String delMusic(){
		try {
			Map<String, Object> param = ParamUtil.buildRequest(ServletActionContext.getRequest());
			upVedioService.deleteVedio(param.get("videoId").toString());
			reParam.put("result", "T");
		} catch (Exception e) {
			reParam.put("result", "F");
		}
		return POST;
	}
	
	/**
	 * 跳转到提交视频页面
	 * @return
	 */
	@Action(value="fromvedio",results={@Result(name="fromvedio",location="authority/fromvedio.jsp")})
	public String redicterFromVedio(){
		return "fromvedio";
	}
	
	
	/**
	 * 上传文件
	 * @return
	 */
	@Action(value="uploadvideo",results={@Result(name="success",location="authority/upvedeo.jsp"),@Result(name="fail",location="authority/uperror.jsp")})
	public String uploadMusicFile(){
		InputStream is=null;  
        OutputStream os=null;  
        File file=null;
        String videoId=ParamUtil.buildUUID();
        /**基础URI*/
        String videoBaseURI=ServletActionContext.getServletContext().getRealPath("\\")+"\\upload\\";
        /**保存的名称*/
        String videoURI=videoBaseURI+videoId+vedioFileName;
        /***/
        String saveVideoURI=ServletActionContext.getServletContext().getContextPath()+"/upload/"+videoId+vedioFileName;
        String imageBaseURI=ServletActionContext.getServletContext().getRealPath("\\")+"\\image\\";
        String imageURI="\\image\\"+imageFileName;
        String saveImage=ServletActionContext.getServletContext().getRealPath("\\")+imageURI;
        Map param=new HashMap<>();
        param.put("id", videoId);
        param.put("videouri", saveVideoURI);
        param.put("name", name);
        param.put("author", author);
        try {  
            is = new BufferedInputStream(new FileInputStream(vedio));
            file=new File(videoBaseURI);
            file.mkdirs();
            os = new BufferedOutputStream(new FileOutputStream(videoURI));  
            byte[] buffer = new byte[1024];  
            int len=0;  
            while((len=is.read(buffer))>0){  
                os.write(buffer,0,len);  
            }   
            if(image!=null){
            	 is=new BufferedInputStream(new FileInputStream(image));
            	 file=new File(imageBaseURI);
            	 file.mkdirs();
            	 os=new BufferedOutputStream(new FileOutputStream(saveImage));
                 buffer = new byte[1024];
                 len=0;  
                 while((len=is.read(buffer))>0){  
                     os.write(buffer,0,len);  
                 }  
                 param.put("imguri", imageURI);
            }else{
            	//TODO 带改动
            	param.put("imguri", "/static/images/p2.jpg");
            }
            /**
             * 保存
             */
            upVedioService.insertVedio(param);
        }catch (Exception e) {
        	return "fail";
		}finally{  
            if(is !=null){try {
				is.close();
			} catch (IOException e) {
				return "fail";
			}}  
            if(os!=null){try {
				os.close();
			} catch (IOException e) {
				return "fail";
			}}  
        }  
		return "success";
	}
	
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getVedioFileName() {
		return vedioFileName;
	}

	public void setVedioFileName(String vedioFileName) {
		this.vedioFileName = vedioFileName;
	}

	public File getVedio() {
		return vedio;
	}

	public void setVedio(File vedio) {
		this.vedio = vedio;
	}

	public String getVedioContentType() {
		return vedioContentType;
	}

	public void setVedioContentType(String vedioContentType) {
		this.vedioContentType = vedioContentType;
	}



	public File getImage() {
		return image;
	}



	public void setImage(File image) {
		this.image = image;
	}



	public String getImageFileName() {
		return imageFileName;
	}



	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}
}
